﻿using System;
using System.Configuration;

namespace Comsenz.Discuz.Configs
{
    /// <summary>
    /// Discuz配置
    /// </summary>
    /// <author>Chiron</author>
    public class DiscuzConfig : ConfigurationSection
    {
        private DiscuzConfig() { }
        /// <summary>
        /// Discuz配置实例
        /// </summary>
        public readonly static DiscuzConfig Instance;
        /// <summary>
        /// 初始化Discuz配置
        /// </summary>
        static DiscuzConfig()
        {
            try
            {
                Instance = ConfigurationManager.GetSection("comsenz.discuz") as DiscuzConfig;
            }
            catch (Exception ex)
            {
#if DEBUG
                throw ex;
#endif
            }

            if (Instance == null) throw new Exception("无法初始化 Comsenz Discuz 配置,请检查是否已正确配置了配置文件.");
        }

        #region 非配置属性
        /// <summary>
        /// 当前使用的链接配置
        /// </summary>
        public DiscuzConnectConfig CurrentConnectConfig
        {
            get
            {
                var connect = Connects[ConnectName] ?? Connects[0];
                return connect;
            }
        }
        #endregion 非配置属性

        #region 配置属性
        /// <summary>
        /// 启用状态
        /// </summary>
        [ConfigurationProperty("enabled", IsRequired = false, DefaultValue = false)]
        public bool Enabled
        {
            get
            {
                return (bool)this["enabled"];
            }
            set
            {
                this["enabled"] = value;
            }
        }
        /// <summary>
        /// 要使用的链接方式名
        /// </summary>
        [ConfigurationProperty("connectName", IsRequired = true)]
        public string ConnectName
        {
            get
            {
                return this["connectName"] as string;
            }
            set
            {
                this["connectName"] = value;
            }
        }

        /// <summary>
        /// 链接方式配置
        /// </summary>
        [ConfigurationProperty("connects", IsRequired = true)]
        public DiscuzConnectCollection Connects
        {
            get
            {
                return this["connects"] as DiscuzConnectCollection;
            }
        }
        /// <summary>
        /// 模块配置集
        /// </summary>
        [ConfigurationProperty("blocks", IsRequired = false)]
        public NameValueConfigurationCollection Blocks
        {
            get
            {
                return this["blocks"] as NameValueConfigurationCollection;
            }
        }
        #endregion 配置属性
    }
}
